package Program.ForgetUI;

import Program.LoginUI.LoginUI;
import Program.SqlTool.AdminDAO;
import Program.SqlTool.UserDAO;
import Program.Tool.Popup;

import javax.swing.*;
import java.awt.event.*;

public class ForgetListener implements ActionListener {
    //登录界面
    private JFrame forgetUI;
    //顶端容器
    private JPanel northPanel;
    //最小化按钮
    private JButton minimizeButton;
    //关闭按钮
    private JButton closeButton;
    //更新按钮
    private JButton renewButton;
    //返回按钮
    private JButton backButton;
    //账号输入框
    private JTextField userNameField;
    //新密码输入框
    private JPasswordField newPasswordField;
    //手机号输入框
    private JTextField phoneField;
    //用户或者管理员登录选择下拉框
    private JComboBox<String> styleBox;
    //手机号输入标签
    private JLabel phoneJLabel;
    //记录鼠标x轴上的位置
    private int mouseAtX = 0;
    //记录鼠标y轴上的位置
    private int mouseAtY = 0;

    public ForgetListener() {
        super();
    }

    public ForgetListener(JFrame forgetUI, JPanel northPanel, JButton minimizeButton, JButton closeButton, JButton renewButton,
                          JButton backButton, JTextField userNameField, JPasswordField newPasswordField,
                          JTextField phoneField, JComboBox<String> styleBox, JLabel phoneJLabel) {
        this.forgetUI = forgetUI;
        this.northPanel = northPanel;
        this.minimizeButton = minimizeButton;
        this.closeButton = closeButton;
        this.renewButton = renewButton;
        this.backButton = backButton;
        this.userNameField = userNameField;
        this.newPasswordField = newPasswordField;
        this.phoneField = phoneField;
        this.styleBox = styleBox;
        this.phoneJLabel = phoneJLabel;
        this.canDraged(this.northPanel);
    }

    //设置窗口移动
    public void canDraged(JPanel p) {
        //创建接收鼠标事件的监听器
        MouseAdapter mouseAdapter = new MouseAdapter() {
            //当鼠标按下时获取窗口的坐标位置
            public void mousePressed(MouseEvent e) {
                mouseAtX = e.getPoint().x;
                mouseAtY = e.getPoint().y;
            }
        };
        MouseMotionAdapter motionAdapter = new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                //设置获取拖拽后的位置并窗口的位置
                forgetUI.setLocation((e.getXOnScreen() - mouseAtX), (e.getYOnScreen() - mouseAtY));
            }
        };
        //给顶部容器添加监听事件
        p.addMouseListener(mouseAdapter);
        p.addMouseMotionListener(motionAdapter);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == renewButton) {
            forgetCheck();
        } else if (e.getSource() == closeButton) {
            //触发关闭事件
            System.exit(0);
        } else if (e.getSource() == minimizeButton) {
            //触发最小化事件
            forgetUI.setExtendedState(JFrame.ICONIFIED);
        } else if (e.getSource() == backButton) {
            //触发返回事件
            SwingUtilities.invokeLater(() -> new LoginUI());
            forgetUI.dispose();
        } else if (e.getSource() == styleBox && styleBox.getSelectedItem().toString().equals("管理员")) {
            //替换手机号标签为密钥
            phoneJLabel.setText("密  钥");
            //设置提示文字
            phoneField.setToolTipText("输入正确的密钥");
            phoneField.setText("");
            phoneField.repaint();
        } else if (e.getSource() == styleBox && styleBox.getSelectedItem().toString().equals("居民")) {
            //替换密钥标签为手机号
            phoneJLabel.setText("手机号");
            phoneField.setText("");
            phoneField.repaint();
        }
    }


    //密码更新检查
    private void forgetCheck() {
        //从用户名框里读取用户名
        String inputUserName = userNameField.getText();
        //从新密码框里读取密码
        char[] tempNewPassword = newPasswordField.getPassword();
        String inputNewPassword = new String(tempNewPassword);
        //从手机号输入框内读取手机号
        String inputPhone = phoneField.getText();
        //提示信息
        String info = "";
        if (inputUserName.length() * inputNewPassword.length() * inputPhone.length() == 0) {
            // 判断所有输入框是否为空
            info = "<html>注册失败,请将所有信息完善</html>";
            Popup.showJDialog(info);
        } else if (!(inputUserName.length() >= 4 && inputUserName.length() <= 17)) {
            //判断用户名格式是否正确，最小长度4，最大长度17
            info = "<html>用户名要求：最小长度为4，最大长度为17</html>";
            Popup.showJDialog(info);
            //清空输入框
            userNameField.setText("");
        } else if (!(inputNewPassword.length() >= 4 && inputNewPassword.length() <= 17)) {
            //判断密码格式是否正确，最小长度4，最大长度17
            info = "<html>密码要求：最小长度为4，最大长度为17</html>";
            Popup.showJDialog(info);
            //清空输入框
            newPasswordField.setText("");
        } else if (!phoneCheck(inputPhone) && styleBox.getSelectedItem().toString().equals("居民")) {
            //判断手机号格式是否正确，长度为11
            info = "<html>请填写正确的手机号</html>";
            Popup.showJDialog(info);
            //清空输入框
            phoneField.setText("");
        } else if (!inputPhone.equals("admin684GBK") && styleBox.getSelectedItem().toString().equals("管理员")) {
            /*模式的切换phoneField这个输入框填写的会更换
            居民界面是手机号，而管理员是密钥，变量名为了减少代码而没有换
             */

            //密钥：admin684GBK
            //判断密钥是否正确
            info = "<html>请输入正确的密钥</html>";
            Popup.showJDialog(info);
            //清空输入框
            phoneField.setText("");
        } else {
            boolean flag;
            //进行更新操作，数据写入数据库
            if (styleBox.getSelectedItem().toString().equals("居民")) {
                flag = UserDAO.updatePassword(inputUserName, inputNewPassword, inputPhone);
                updateDetermine(flag, info);
            } else {
                flag = AdminDAO.updatePassword(inputUserName, inputNewPassword);
                updateDetermine(flag, info);
            }
        }
    }

    //手机号格式检查
    private boolean phoneCheck(String inputPhone) {
        String regex = "^1(?:(?:3[\\d])|(?:4[5-7|9])|(?:5[0-3|5-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[1|8|9]))\\d{8}$";
        boolean flag = inputPhone.matches(regex);
        return flag;
    }

    //更新操作判断方法
    private void updateDetermine(boolean flag, String info) {
        if (flag) {
            info = "<html>密码更新成功</html>";
            Popup.showJDialog(info);
            SwingUtilities.invokeLater(() -> new LoginUI());
            forgetUI.dispose();
        }else {
            info = "<html>密码更新失败，账号或者手机号输入错误</html>";
            Popup.showJDialog(info);
            //清空输入框
            userNameField.setText("");
            //清空输入框
            newPasswordField.setText("");
            //清空输入框
            phoneField.setText("");
        }

    }
}
